package com.vf.admin.utils.sendsms;
import cn.hutool.core.util.ObjectUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class DuanXinBaoSms {
    public static void main(String[] args) {
        String testUsername = "zhiyuapp"; //在短信宝注册的用户名
        String testPassword = "Zypass007!"; //在短信宝注册的密码
        String Phone = "13275012315";
        String smsContent = "【知遇】您的验证码123456，请勿向任何人提供您收到的短信验证码！"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到

        String httpUrl = "http://api.smsbao.com/sms";

        StringBuffer httpArg = new StringBuffer();
        httpArg.append("u=").append(testUsername).append("&");
        httpArg.append("p=").append(md5(testPassword)).append("&");
        httpArg.append("m=").append(Phone).append("&");
        httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
        String result = request(httpUrl, httpArg.toString());
        System.out.println(result);
    }

    public static void send(String mobile, String code, String areaCode,String packageName) {

        if (ObjectUtil.isNotEmpty(packageName)){


            if (packageName.equals("popose")){

                String testUsername = "zhiyuapp"; //在短信宝注册的用户名
                String testPassword = "Zypass007!"; //在短信宝注册的密码
                String phone = mobile;
                String smsContent = "【popose】您的验证码"+code+"。请勿向任何人提供您收到的短信验证码！"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
                String httpUrl = "http://api.smsbao.com/sms";

                //國際
                if (!("".equals(areaCode) || "+86".equals(areaCode))){
                    httpUrl = "http://api.smsbao.com/wsms";
                    phone = areaCode + mobile;
                }

                StringBuffer httpArg = new StringBuffer();
                httpArg.append("u=").append(testUsername).append("&");
                httpArg.append("p=").append(md5(testPassword)).append("&");
                httpArg.append("m=").append(encodeUrlString(phone, "UTF-8")).append("&");
                httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
                String result = request(httpUrl, httpArg.toString());
            }else if (packageName.equals("ReTu")) {
                String testUsername = "zhiyuapp"; //在短信宝注册的用户名
                String testPassword = "Zypass007!"; //在短信宝注册的密码
                String phone = mobile;
                String smsContent = "【ReTu】您的验证码"+code+"，请勿向任何人提供您收到的短信验证码！"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
                String httpUrl = "http://api.smsbao.com/sms";

                //國際
                if (!("".equals(areaCode) || "+86".equals(areaCode))){
                    httpUrl = "http://api.smsbao.com/wsms";
                    phone = areaCode + mobile;
                }

                StringBuffer httpArg = new StringBuffer();
                httpArg.append("u=").append(testUsername).append("&");
                httpArg.append("p=").append(md5(testPassword)).append("&");
                httpArg.append("m=").append(encodeUrlString(phone, "UTF-8")).append("&");
                httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
                String result = request(httpUrl, httpArg.toString());

            }else if (packageName.equals("lalaq")) {
                String testUsername = "zhiyuapp"; //在短信宝注册的用户名
                String testPassword = "Zypass007!"; //在短信宝注册的密码
                String phone = mobile;
                String smsContent = "【lalaq】您的验证码"+code+"，请勿向任何人提供您收到的短信验证码！"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
                String httpUrl = "http://api.smsbao.com/sms";

                //國際
                if (!("".equals(areaCode) || "+86".equals(areaCode))){
                    httpUrl = "http://api.smsbao.com/wsms";
                    phone = areaCode + mobile;
                }

                StringBuffer httpArg = new StringBuffer();
                httpArg.append("u=").append(testUsername).append("&");
                httpArg.append("p=").append(md5(testPassword)).append("&");
                httpArg.append("m=").append(encodeUrlString(phone, "UTF-8")).append("&");
                httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
                String result = request(httpUrl, httpArg.toString());

            }else if (packageName.equals("aita")) {
                String testUsername = "zhiyuapp"; //在短信宝注册的用户名
                String testPassword = "Zypass007!"; //在短信宝注册的密码
                String phone = mobile;
                String smsContent = "【aita】您的验证码"+code+"，请勿向任何人提供您收到的短信验证码！"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
                String httpUrl = "http://api.smsbao.com/sms";

                //國際
                if (!("".equals(areaCode) || "+86".equals(areaCode))){
                    httpUrl = "http://api.smsbao.com/wsms";
                    phone = areaCode + mobile;
                }

                StringBuffer httpArg = new StringBuffer();
                httpArg.append("u=").append(testUsername).append("&");
                httpArg.append("p=").append(md5(testPassword)).append("&");
                httpArg.append("m=").append(encodeUrlString(phone, "UTF-8")).append("&");
                httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
                String result = request(httpUrl, httpArg.toString());

            }
            else {
                String testUsername = "zhiyuapp"; //在短信宝注册的用户名
                String testPassword = "Zypass007!"; //在短信宝注册的密码
                String phone = mobile;
                String smsContent = "【旅友圈】您的验证码是"+code+"。如非本人操作，请忽略本短信"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
                String httpUrl = "http://api.smsbao.com/sms";

                //國際
                if (!("".equals(areaCode) || "+86".equals(areaCode))){
                    httpUrl = "http://api.smsbao.com/wsms";
                    phone = areaCode + mobile;
                }

                StringBuffer httpArg = new StringBuffer();
                httpArg.append("u=").append(testUsername).append("&");
                httpArg.append("p=").append(md5(testPassword)).append("&");
                httpArg.append("m=").append(encodeUrlString(phone, "UTF-8")).append("&");
                httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
                String result = request(httpUrl, httpArg.toString());
            }




        }else {

            String testUsername = "zhiyuapp"; //在短信宝注册的用户名
            String testPassword = "Zypass007!"; //在短信宝注册的密码
            String phone = mobile;
            String smsContent = "【旅友圈】您的验证码是"+code+"。如非本人操作，请忽略本短信"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
            String httpUrl = "http://api.smsbao.com/sms";

            //國際
            if (!("".equals(areaCode) || "+86".equals(areaCode))){
                httpUrl = "http://api.smsbao.com/wsms";
                phone = areaCode + mobile;
            }

            StringBuffer httpArg = new StringBuffer();
            httpArg.append("u=").append(testUsername).append("&");
            httpArg.append("p=").append(md5(testPassword)).append("&");
            httpArg.append("m=").append(encodeUrlString(phone, "UTF-8")).append("&");
            httpArg.append("c=").append(encodeUrlString(smsContent, "UTF-8"));
            String result = request(httpUrl, httpArg.toString());
        }

    }


    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }

    public static String encodeUrlString(String str, String charset) {
        String strret = null;
        if (str == null)
            return str;
        try {
            strret = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strret;
    }
}
